<?php
namespace app\wf\controller;



use BP\DA\DBAccess;
use BP\DA\Paras;
use BP\Web\WebUser;
use think\Db;
use think\Controller;
class Test extends Controller{
    const a=3;
    public function test() {
//         $conn = DBAccess::getGetAppCenterDBConn_MySQL();
//         $sth = $conn->prepare('insert into tt (`id`) values (2)');
//         $sth->execute();
//         $sql = 'insert into tt (`id`) values (2)';
//         $map = [];
//         DBAccess::RunSQL_String('insert into tt (`id`) values (4)');
//         $result = '';
//         return json_encode($result);
    }

    public function testname() {
        //        return view("../hello");
//         return phpinfo();
    }
    
    public function index() {
//         $config = require __DIR__ . '/../../config.php';
//         $database = require __DIR__ . '/../../database.php';
//         $ccflow = $config['ccflow'];
//         $AppCenterDBType = $ccflow['AppCenterDBType'];
//         $connection = $database;
        DBAccess::RunSQL_String("insert into tt values(6);");
        return json_encode(Db::table('port_emp')->find(), JSON_FORCE_OBJECT);
    }
    
    public function dbaccess(){
        #region 执行不带有参数.
        // 执行Insert ,delete, update 语句.
        $result = DBAccess::RunSQL_String("DELETE FROM Port_Emp WHERE 1=2");
        echo ' result-- ' . $result . '::result-';
        // 执行多个sql
        $sqls = "DELETE FROM Port_Emp WHERE 1=2";
        $sqls .= "@DELETE FROM Port_Emp WHERE 1=2";
        $sqls .= "@DELETE FROM Port_Emp WHERE 1=2";
        DBAccess::RunSQLs($sqls);
        
        //执行查询返回datatable.
        $sql = "SELECT * FROM Port_Emp";
        $dt = DBAccess::RunSQLReturnTable_String($sql);
        echo $dt . '::datatable-';
        //执行查询返回 string 值.
        $sql = "SELECT FK_Dept FROM Port_Emp WHERE No='" . WebUser::getNo() . "'";
        $fk_dept = DBAccess::RunSQLReturnString($sql);
        echo $fk_dept . '::fk_dept-';
        //执行查询返回 int 值. 也可以返回float, string
        $sql = "SELECT count(No) as Num FROM Port_Emp ";
        $empNum = DBAccess::RunSQLReturnValInt($sql);
        echo $empNum . '::empNum-';
        //运行存储过程.
        // string spName = "MySp";
        //BP.DA.DBAccess.RunSP(spName);
        #endregion 执行不带有参数.
        
        #region 执行带有参数.
        // 执行Insert ,delete, update 语句.
        // 已经明确数据库 sqlserver 类型.
        $ps = new Paras();
        $ps->SQL = "DELETE FROM Port_Emp WHERE No=@UserNo";
        ps.Add("UserNo", "zhangsan");
        DBAccess::RunSQL($ps);
        
        // 不知道数据库类型.
        $ps = new Paras();
        $ps->SQL = "DELETE FROM Port_Emp WHERE No=" + BP.Sys.SystemConfig.AppCenterDBVarStr + "UserNo";
        $ps::Add("UserNo", "abc");
        DBAccess::RunSQL($ps);
        
        //执行查询返回datatable.
        $ps = new Paras();
        $ps->SQL = "SELECT * FROM Port_Emp WHERE FK_Dept=@DeptNoVar";
        $ps::Add("DeptNoVar", "0102");
        $dtEmps = DBAccess::RunSQLReturnTable($ps); //DataTable 
        
        foreach ($dtEmps->Rows as $dr) //DataRow 
        {
            $no = $dr["No"]::ToString();
            $name = $dr["Name"]::ToString();
        }
        
        //运行存储过程.
        //ps = new Paras();
        //ps.Add("DeptNoVar", "0102");
        //spName = "MySp";
        //BP.DA.DBAccess.RunSP(spName, ps);
        #endregion 执行带有参数.
        
        
        #region 向数据库存储、读写文件.
        //写入实例.
        $tmpFile = "c:\\111.txt";
        DBAccess::SaveFileToDB($tmpFile, "WF_Flow", "No", "001", "FlowJson"); // FlowJson是一个已经存在的img字段.
        
        //读取文件到指定的位置.
        $saveToFile = "c:\\222.txt";
        //  string byt = DBAccess::GetBigTextFromDB(saveToFile, "WF_Flow", "No", "001", "FlowJson");
        // FlowJson是一个已经存在的img字段, 系统就会生成一个 temp文件在指定的路径下. 返回的是一个byt 类型的数据流.
        #endregion 向数据库存储文件.
    }
}
